package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.UserMapper;
import com.itheima.pojo.CategoryMenu;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import com.itheima.vo.CategoryMenuVO;
import com.itheima.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;


import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {


    @Autowired
    private UserMapper userMapper;

    public int removeUser(String id) {
        return userMapper.deleteUser(id);
    }

    @Override
    public int updateUser(User user) {
        return userMapper.updateByPrimaryKey(user);
    }



    @Override
    public int resetUserPassword(String uid, String passWord) {
        return userMapper.resetUserPassword(uid, passWord);
    }

    @Override
    public Page<User> getList(UserVO userVO) {
        if (userVO!=null){
            //设置默认当前页
            if (userVO.getCurrentPage() == 0 || "".equals(userVO.getCurrentPage())) {
                userVO.setCurrentPage(1);
            }
            //设置默认每页条数
            if (userVO.getPageSize() == 0 || "".equals(userVO.getPageSize())) {
                userVO.setPageSize(10);
            }
        }else {
            userVO = new UserVO();
            userVO.setCurrentPage(1);
            userVO.setPageSize(10);
        }
        Integer currentPage = userVO.getCurrentPage();
        Integer pageSize = userVO.getPageSize();
        PageHelper.startPage(currentPage,pageSize);
        Example example = createExample(userVO);
        return (Page<User>) userMapper.selectByExample(example);
    }


    @Override
    public Integer getUserCount() {
        User user = new User();
        user.setStatus(1);
        return userMapper.selectCount(user);
    }

    /**
     * 构建查询对象
     *
     * @param userVO
     * @return
     */
    private Example createExample(UserVO userVO) {
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        if (userVO != null) {
            // 唯一uid
            if (userVO.getUid() != null && !"".equals(userVO.getUid())) {
                criteria.andLike("uid", "%" + userVO.getUid() + "%");
            }
            // 用户名
            if (userVO.getUserName() != null && !"".equals(userVO.getUserName())) {
                criteria.andLike("userName", "%" + userVO.getUserName() + "%");
            }
            // 密码
            if (userVO.getPassWord() != null && !"".equals(userVO.getPassWord())) {
                criteria.andLike("passWord", "%" + userVO.getPassWord() + "%");
            }

            // 性别(1:男2:女)
            if (userVO.getGender() != null) {
                criteria.andEqualTo("gender", userVO.getGender());
            }


        }
        return example;
    }

}
